package com.springdata.mongodb;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springdata.domain.Contact;
import com.springdata.domain.Person;
import com.springdata.domain.User;
import com.springdata.repository.ContactRepository;
import com.springdata.repository.PersonRepository;
import com.springdata.repository.UserRepository;

@Controller
public class MongoController {
	private static final Logger logger = LoggerFactory.getLogger(MongoController.class);
	
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private ContactRepository contactRepository;
	@Autowired
	private PersonRepository repository;
	
	//로그인화면 - 내선번호선택
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String login(Locale locale, Map<String, Object> map){
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		
		Iterable<User> users = userRepository.findAll();
		
		map.put("serverTime", formattedDate );
		map.put("users", users);
		return "login";
	}
	
	//메인화면
	@RequestMapping(value="/contact/{userId}/{extNo}", method=RequestMethod.GET) 
	public String getContact(@PathVariable String userId, @PathVariable String extNo, Map<String, Object> map, HttpSession session){
//		Page<Contact> page = contactRepository.findAll(new PageRequest(0, 5));
//		List<Contact> contacts = page.getContent();
		session.setAttribute("extNo", extNo);
		session.setAttribute("userId", userId);
		Iterable<Contact> contacts = contactRepository.findAll();
		map.put("contacts", contacts);
		return "contact";
	}
	
	//관리화면
	//추가
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addContact(){
		this.contactRepository.save(new Contact("리파인", "전산담당", "최영남", "선임", "02-2189-8714", "010-3170-1788", "young713@refine.co.kr", "localtest"));
		return "add-contact";
	}
	//수정
	//삭제
	
	////////////////////////////////////////이하 테스트//////////////////////////////////////
	
	@RequestMapping("/findAll")
	public void findAll() {
		Page<Person> page = repository.findAll(new PageRequest(0, 3));
		List<Person> people = page.getContent();
		System.out.println("[people]="+people);
	}
	
	@RequestMapping("/save")
	public void save() {
		Person p = new Person("young", 30);
		Person p1 = this.repository.save(p);
		
		ObjectId id = new ObjectId(p.getId());
		
		Date date = new Date(id.getTime());
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.KOREA);
		
		String formattedDate = dateFormat.format(date);
		
		logger.info("["+formattedDate+"] "+p1.toString());
	}
	
	@RequestMapping("/deleteAll")
	public void deleteAll() {
		repository.deleteAll();
	}
}
